﻿using System.Globalization;
using System.Web.Compilation;
using System.Web.Mvc;

namespace CasualisHTMLGames.Code.Localization {
    public static class LanguageResourceExtension {
        public static string GetResource(this HtmlHelper html, string expr, params object[] args) {
            string path = ((RazorView)html.ViewContext.View).ViewPath;
            ResourceExpressionFields fields = (ResourceExpressionFields)(new ResourceExpressionBuilder()).ParseExpression(
                expr,
                typeof(string),
                new ExpressionBuilderContext(path)
                );
            if (!string.IsNullOrEmpty(fields.ClassKey)) {
                return string.Format((string)html.ViewContext.HttpContext.GetGlobalResourceObject(fields.ClassKey, fields.ResourceKey, CultureInfo.CurrentUICulture), args);
            } else {
                return string.Format((string)html.ViewContext.HttpContext.GetLocalResourceObject(path, fields.ResourceKey, CultureInfo.CurrentUICulture), args);
            }
        }
    }
}